/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.test.Register;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author A0109314N
 */
@WebServlet("/register")
public class RegisterServlet extends HttpServlet 
{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
    {
         Cookie cookies[]=req.getCookies();
        
//        boolean foundcookie=false;
//        for (Cookie c : cookies) 
//        {
//            
//            
//        }
        
        resp.sendRedirect("RegistrationForm.html");

//        String name;
//        HttpSession session=req.getSession();
//        for (Cookie c : cookies) 
//        {
//            if("name".equals(c.getName()))
//            {
//                name=c.getValue();
//                session.setAttribute("name", name);
//                
//            }
//            
//        }
        
        
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
    {
        String name=req.getParameter("name");
        String Country=req.getParameter("country");
        String email=req.getParameter("email");
        String eid=req.getParameter("email");
        String newsletter=req.getParameter("newsletter");
        String[] topics=req.getParameterValues("topics[]");
        String gender=req.getParameter("gender");
        resp.setStatus(HttpServletResponse.SC_CREATED);
        resp.setContentType("text/html");
        //Printwriter pw=resp.getWriter();
        PrintWriter pw=resp.getWriter();
        HttpSession session=req.getSession();
        Cookie cookies[]=req.getCookies();
        boolean foundcookie=false;
        
        
        String button=req.getParameter("button");
        if("submit".equals(button))
        {
            if(newsletter!=null)
                
            {
                pw.println("<html><body><br>");
                pw.println("Our monthly newsletter will be sent to your email: "+eid);
                
                pw.println("</body></html>");
            }
            else
            {
                pw.println("<html><body>"+"Thank you for registering with us");
                for (String top : topics) 
                {
                    pw.println(top+"<br>");
                    
                }
            }
            
        }
        else if("save".equals(button))
        {
            pw.println("save");
            Cookie cname=new Cookie("name",req.getParameter("name"));
            Cookie ccountry=new Cookie("country", req.getParameter("country"));
            resp.addCookie(cname);
            resp.addCookie(ccountry);
            
            
            
        }
      
        
        
        
    }
  
    
    
    
}
